<?php
namespace app\common\client;

use think\facade\Config;
use think\facade\Log;

class SaiKaSMS {
    
    /***
     * 套餐列表查询
     * @param String $cardCode
     * @return array|mixed
     */
    public function package(String $cardCode){
        $data = [];
        $data['partnercode'] = Config::get('saika.partnercode');
        $data['servicecode'] = Config::get('saika.servicecode');
        $data['requesttime'] = date('YmdHis').'2500000001';
        $data['key'] = $cardCode;
        $string = Config::get('saika.password').$data['requesttime'].$data['key'];
        $data['sign'] = md5(strtolower($string));
        
        return $this->send('/M2MSearchInfo.ashx', $data);
    }
    
    /**
     * 卡状态
     * @param String $cardCode
     * @return array|mixed
     */
    public function realTime(String $cardCode){
        $data = [];
        $data['partnercode'] = Config::get('saika.partnercode');
        $data['servicecode'] = Config::get('saika.servicecode');
        $data['requesttime'] = date('YmdHis').'2500000001';
        $data['key'] = $cardCode;
        $data['status'] = 2;
        $string = Config::get('saika.password').$data['requesttime'].$data['key'].$data['status'];
        $data['sign'] = md5(strtolower($string));
        
        return $this->send('/M2MSearchTermInfo.ashx', $data);
        
    }
    
    /**
     * 流量查询
     * @param String $simId
     * @return array|mixed
     */
    public function cardFlow(String $simId){
        $data = [];
        $data['partnercode'] = Config::get('saika.partnercode');
        $data['servicecode'] = Config::get('saika.servicecode');
        $data['requesttime'] = date('YmdHis').'2500000001';
        $data['key'] = $simId;
        $string = Config::get('saika.password').$data['requesttime'].$data['key'];
        $data['sign'] = md5(strtolower($string));
        
        return $this->send('/M2MSurplusFlow.ashx', $data);
    }
    
    /**
     * 套餐列表查询
     * @param String $simId
     * @return array|mixed
     */
    public function packageInfo(String $simId) {
        $data = [];
        $data['partnercode'] = Config::get('saika.partnercode');
        $data['servicecode'] = Config::get('saika.servicecode');
        $data['requesttime'] = date('YmdHis').'2500000001';
        $data['key'] = $simId;
        $string = Config::get('saika.password').$data['requesttime'].$data['key'];
        $data['sign'] = md5(strtolower($string));
        
        return $this->send('/M2MSearchInfo.ashx', $data);
    }
    
    /**
     * 查询短信发送结果
     * @return array|mixed
     */
    public function querySendStatus(String $sendKey){
        $data = [];
        $data['partnercode'] = Config::get('saika.partnercode');
        $data['servicecode'] = Config::get('saika.servicecode');
        $data['requesttime'] = date('YmdHis').'2500000001';
        $data['sendkey'] = $sendKey;
        $string = Config::get('saika.password').$data['requesttime'].$data['sendkey'];
        $data['sign'] = md5(strtolower($string));

        return $this->send('/M2MHistorySend.ashx', $data);
    }
    
    public function send($path,$json) {
       $url = Config::get('saika.url').$path;
       $data = json_encode($json);
       $curl = curl_init($url);  // 初始化 cURL 会话
       curl_setopt($curl, CURLOPT_HEADER, false);  // 启用时会将头文件的信息作为数据流输出。
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // TRUE 将curl_exec()获取的信息以字符串返回，而不是直接输出。
       curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));  //设置 HTTP 头字段的数组。格式： array('Content-type: text/plain', 'Content-length: 100')
       curl_setopt($curl, CURLOPT_POST, true);     //TRUE 时会发送 POST 请求
       curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  // 全部数据使用HTTP协议中的 "POST" 操作来发送。value必须是个数组
       $content = curl_exec($curl);  // 执行 cURL 会话 , 应该在初始化一个 cURL 会话并且全部的选项都被设置后被调用。
       if(curl_errno($curl)){
           $content = [];
           Log::record(curl_errno($curl),'error');
       }else{
           
           $info = curl_getinfo($curl);   // 获取一个cURL连接资源句柄的信息(获取最后一次传输的相关信息)
           
           if($info['http_code'] !== 200){
               
               $content = [];
               Log::record('对方服务器错误','error');
               
           }
       }
       
       curl_close($curl); // 关闭 cURL 会话
       return $content;
    }
}